﻿Imports System.Security.Cryptography

Public Class login
    Public intentos As Integer = 0
    Public Sub entrar()

        Dim verificacontra As String = MD5EncryptPass(contraseña.Text.Trim + "jova889")
        Dim verificauser As String = usuario.Text.Trim

        Dim bduser As Integer = Me.UsuariosTableAdapter.ScalarQueryConsultaUser(verificauser)
        Dim bdcontra As String = Me.UsuariosTableAdapter.ScalarQueryConsultaContra(verificauser)


        If bduser = 1 And verificacontra = bdcontra Then



            My.Forms.MDI.DefaultLookAndFeel1.LookAndFeel.SkinName = Me.UsuariosTableAdapter.ScalarQuerySkin(verificauser)

            Dim bdiduser As Integer = Me.UsuariosTableAdapter.ScalarQueryConsultaIdIser(verificauser)
            Dim bdrol As Integer = Me.UsuariosTableAdapter.ScalarQueryConsultaRol(verificauser)
            My.Forms.MDI.Iduser.Caption = bdiduser
            My.Forms.MDI.nombreuser.Caption = verificauser
            My.Forms.MDI.Idrol.Caption = bdrol
            MDI.Show()



        Else

            'MsgBox(System.Environment.MachineName)


            intentos = intentos + 1
            If intentos = 4 Then
                MsgBox("El número de intentos para ingresar al Sistema se han agotado, el Sistema se cerrará por razones de seguridad.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Intentos agotados")
                Application.Exit()
            End If

            MsgBox("El nombre de Usuario y/o Contraseña son inválidos, favor inténtelo nuevamente.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error al ingresar al Sistema")
        End If
        '  MsgBox(Me.UsuariosTableAdapter.ScalarQuerySkin(usuario.Text))
    End Sub
    Public Sub entrarcontra()
        If c1.Text.Trim = c2.Text.Trim Then
            Dim cadena As String
            cadena = String.Format("Para finalizar la instalación y configuración del Sistema, se creará un Usuario Administrador con la contraseña introducida en los cuadros de texto, sus credenciales son: Usuario=Admin  Contraseña={0}", c1.Text)
            MsgBox(cadena, MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Bienvenido Molécula")

            Try
                Me.RolesTableAdapter11.InsertQueryPrimero(1, "Administrador", "Usuario con control total")
                Me.UsuariosTableAdapter.InsertQueryPrimero(1, "Admin", MD5EncryptPass(c1.Text + "jova889"), "", "Money Twins", "", "", "Usuario Administrador", 1)
            Catch ex As Exception
                MsgBox("Error al crear el Super Usuario, favor consulte al Técnico Programador")
            End Try



            My.Forms.MDI.DefaultLookAndFeel1.LookAndFeel.SkinName = "Money Twins"
            My.Forms.MDI.Iduser.Caption = 1
            My.Forms.MDI.nombreuser.Caption = "Admin"
            My.Forms.MDI.Idrol.Caption = 1
            MDI.Show()






        Else
            MsgBox("Las contraseñas no coinciden, favor inténtelo nuevamente.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error al ingresar al Sistema")
        End If
    End Sub
    Private Function MD5EncryptPass(ByVal StrPass As String) As String
        Dim PasConMd5 As String = ""
        Dim md5 As New MD5CryptoServiceProvider
        Dim bytValue() As Byte
        Dim bytHash() As Byte
        Dim i As Integer

        bytValue = System.Text.Encoding.UTF8.GetBytes(StrPass)

        bytHash = md5.ComputeHash(bytValue)
        md5.Clear()

        For i = 0 To bytHash.Length - 1
            PasConMd5 &= bytHash(i).ToString("x").PadLeft(2, "0")
        Next

        Return PasConMd5

    End Function

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        entrarcontra()

    End Sub

    Private Sub login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'BDPublicidadDataSet.usuarios' table. You can move, or remove it, as needed.
        Me.UsuariosTableAdapter.Fill(Me.DbFireAndIceDataSet.usuarios)
        'TODO: This line of code loads data into the 'BDPublicidadDataSet.usuarios' table. You can move, or remove it, as needed.
        Me.UsuariosTableAdapter.Fill(Me.DbFireAndIceDataSet.usuarios)
        Conexion.Close()


        If Me.UsuariosTableAdapter.ScalarQueryCuantosHay() = 0 Then
            Panel1.Visible = False
            Panel2.Visible = True
            c1.Focus()
        Else
            Panel1.Visible = True
            Panel2.Visible = False
            usuario.Focus()
        End If

    End Sub

    Private Sub UsuariosBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.UsuariosBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click

        entrar()
    End Sub

    Private Sub usuario_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles usuario.Click
        If usuario.Text = "Usuario" Then
            usuario.Text = ""
        End If
    End Sub



    Private Sub contraseña_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles contraseña.Click
        If contraseña.Text = "Contraseña" Then
            contraseña.Text = ""
            contraseña.PasswordChar = "*"
        End If
    End Sub



    Private Sub c1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles c1.Click
        If c1.Text = "Contraseña" Then
            c1.Text = ""
            c1.PasswordChar = "*"
        End If
    End Sub



    Private Sub c2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles c2.Click
        If c2.Text = "Repita la contraseña" Then
            c2.Text = ""
            c2.PasswordChar = "*"
        End If
    End Sub


    Private Sub c2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles c2.GotFocus
        If c2.Text = "Repita la contraseña" Then
            c2.Text = ""
            c2.PasswordChar = "*"
        End If
    End Sub



    Private Sub contraseña_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles contraseña.GotFocus
        If contraseña.Text = "Contraseña" Then
            contraseña.Text = ""
            contraseña.PasswordChar = "*"
        End If
    End Sub






    Private Sub usuario_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles usuario.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            entrar()
        End If
    End Sub



    Private Sub c1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles c1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            entrarcontra()
        End If
    End Sub

    Private Sub contraseña_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles contraseña.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            entrar()
        End If
    End Sub

    Private Sub c2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles c2.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            entrarcontra()
        End If
    End Sub
End Class